<?php

	include_once '../com/mss/model/User.php';
	include_once '../com/mss/accessor/UserAccessor.php';

	class LoginAction {
		
		public function login($login_id, $password) {
			$errors = $this->validate($login_id, $password);
			if (count($errors, 1) == 0) {
				$results['success'] = "YES";
				session_start();
				$_SESSION['LOGIN_ID'] = $login_id;
				$_SESSION['LAST_ACTIVE_TIME'] = time();
				header("Location: index.php");
			} else {
				$results['success'] = "NO";
				$results['errors'] = $errors;
			}
			return $results;
		}
		
		private function validate($login_id, $password) {
			$errors = array();
			if (empty($login_id)) {
				array_push($errors, "Please enter the user name");
			}
			if (empty($password)) {
				array_push($errors, "Please enter the password");
			}
			if (!empty($login_id) && !empty($password)) {
				$user_accessor = new UserAccessor();
				$password = sha1($password, false);
				$count = $user_accessor->findByLoginIDAndPassword($login_id, $password);
				if ($count == 0) {
					array_push($errors, "Please input your login id and password correctly");
				}
			}
			return $errors;		
		}
		
	}

?>